package com.qbw.app.module.common.fragment;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.View;

import com.lib.util.ResUtil;
import com.qbw.app.Constant;
import com.qbw.app.R;
import com.qbw.app.databinding.CommonFragmentLoopItemSelectBinding;
import com.qbw.app.manager.MapManager;
import com.qbw.app.manager.UnitManager;
import com.qbw.core.base.BaseBottomDialogFragment;
import com.qbw.spm.P;


import java.util.ArrayList;

/**
 * @author QBW
 * @date 2019-09-21
 */
public class LoopItemSelectDialogFragment extends BaseBottomDialogFragment<CommonFragmentLoopItemSelectBinding> implements View.OnClickListener {

    public static final String KEY_INIT_POS = "key_init_pos";
    public static final String KEY_STRING_ITEMS = "key_string_items";
    public static final String KEY_TYPE = "key_type";

    public static final int TYPE_MAP_SELECT = 1;
    public static final int TYPE_UNI_SELECT = 2;

    private int mType;
    private int mInitPos;
    private ArrayList<String> mItems;

    private Object mListener;

    public static LoopItemSelectDialogFragment createInstanceForMapSelect() {
        ArrayList<String> maps = new ArrayList<>();
        maps.add(ResUtil.getString(R.string.baidu_map));
        maps.add(ResUtil.getString(R.string.google_map));
        String map = P.getString(Constant.KEY_MAP, Constant.MAP_DEFAULT);
        boolean isBaidu = map.equals(Constant.MAP_BAIDU);
        return createInstance(TYPE_MAP_SELECT, maps, isBaidu ? 0 : 1);
    }

    public static LoopItemSelectDialogFragment createInstanceForUnitSelect() {
        ArrayList<String> units = new ArrayList<>();
        units.add(ResUtil.getString(R.string.unit_kph));
        units.add(ResUtil.getString(R.string.unit_mph));
        boolean isKph = P.getString(Constant.KEY_UNIT, Constant.UNIT_DEFAULT).equals(Constant.UNIT_KPH);
        return createInstance(TYPE_UNI_SELECT, units, isKph ? 0 : 1);
    }

    public static LoopItemSelectDialogFragment createInstance(int type, ArrayList<String> items, int initPos) {
        Bundle args = new Bundle();
        args.putInt(KEY_TYPE, type);
        args.putStringArrayList(KEY_STRING_ITEMS, items);
        args.putInt(KEY_INIT_POS, initPos);
        LoopItemSelectDialogFragment fragment = new LoopItemSelectDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected int onGetLayoutId() {
        return R.layout.common_fragment_loop_item_select;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = savedInstanceState == null ? getArguments() : savedInstanceState;
        mType = args.getInt(KEY_TYPE);
        mItems = args.getStringArrayList(KEY_STRING_ITEMS);
        mInitPos = args.getInt(KEY_INIT_POS, 0);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.lv.setNotLoop();
        mBinding.lv.setItems(mItems);
        mBinding.lv.setInitPosition(mInitPos);
        mBinding.tvOk.setOnClickListener(this);
        mBinding.tvCancel.setOnClickListener(this);
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(KEY_TYPE, mType);
        outState.putStringArrayList(KEY_STRING_ITEMS, mItems);
        outState.putInt(KEY_INIT_POS, mInitPos);
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_ok) {
            if (mType == TYPE_MAP_SELECT) {
                String map = mItems.get(mBinding.lv.getSelectedItem());
                boolean isBaidu = map.equals(ResUtil.getString(R.string.baidu_map));
                MapManager.getInstance().setMap(isBaidu);
            } else if (mType == TYPE_UNI_SELECT) {
                boolean isKph = mItems.get(mBinding.lv.getSelectedItem()).equals(ResUtil.getString(R.string.unit_kph));
                UnitManager.getInstance().setUnit(isKph);
            }
            if (mListener instanceof Listener) {
                ((Listener) mListener).onSelectItem(mType, mItems, mBinding.lv.getSelectedItem());
            }

            dismiss();
        } else if (vid == R.id.tv_cancel) {
            dismiss();
        }
    }

    public LoopItemSelectDialogFragment setListener(Listener listener) {
        mListener = listener;
        return this;
    }

    public interface Listener {
        void onSelectItem(int type, ArrayList<String> items, int selectPos);
    }
}
